package kxfang.com.common.bean;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import kxfang.com.base.utils.GsonUtils;
import kxfang.com.common.utils.MyUtils;


/**
 * 商品详情
 */
public class GoodsDetailModel implements Parcelable {

    private String ID;
    private String Title;
    private String DisCountNum;//折扣数
    private String MonthSaleNum;//月售数量
    private String IPrice;//原价
    private String Statu;//0 未上架 1 已上架
    private String ShareTitle;//分享标题
    private String ShareImg;//分享图片 全路径
    private String ShareDesc;//分享描述
    private String url;//分享链接
    private List<ImageModel> ImgList;
    private List<ImageModel> ContentImgList;
    private String LimitCount;// 0，表示不限购
    private int IsRecommends;//店长推荐 0 不推荐 1 推荐
    private boolean isCheck;// 不是后台返回的
    private String ClassID;//类别ID
    private String ClassName;//类别名称
    private String CoverUrl;//封面图
    private String CreateTime;//创建时间
    private String DisCountPrice;//折扣价
    private int Num;//数量
    private String StoreID;
    private String DistributionFee;// 包邮
    private String StartPrice;//配送费
    private String GoodsName;//商品名
    private String GoodsID;// 当在购物车的时候ID表示购物车id
    private String ShopCartID;//购物车id
    private String GoodsNum;
    private String ShowOrder;
    private String PackingFee;
    private String CMemo;
    private String ServiceContent;
    private String TermOfValidity;
    private String UsageRules;
    private String RefundRules;
    private String SKU;
    private List<GoodsSKUModel> goodsSKUModels;
    private String strSKU;
    private String titleSKU;
    private String splitChar = "、";

    protected GoodsDetailModel(Parcel in) {
        ID = in.readString();
        Title = in.readString();
        DisCountNum = in.readString();
        MonthSaleNum = in.readString();
        IPrice = in.readString();
        Statu = in.readString();
        ShareTitle = in.readString();
        ShareImg = in.readString();
        ShareDesc = in.readString();
        url = in.readString();
        LimitCount = in.readString();
        IsRecommends = in.readInt();
        isCheck = in.readByte() != 0;
        DisCountPrice = in.readString();
        Num = in.readInt();
        StoreID = in.readString();
        GoodsID = in.readString();
        ShopCartID = in.readString();
        GoodsName = in.readString();
        GoodsNum = in.readString();
        ShowOrder = in.readString();
        PackingFee = in.readString();
        DistributionFee = in.readString();
        StartPrice = in.readString();
        CMemo = in.readString();
        strSKU = in.readString();
        titleSKU = in.readString();
    }

    public GoodsDetailModel() {

    }

    public GoodsDetailModel(String ID, String limitCount, String coverUrl, String disCountPrice, int num, String storeID, String goodsName) {
        this.ID = ID;
        LimitCount = limitCount;
        CoverUrl = coverUrl;
        DisCountPrice = disCountPrice;
        Num = num;
        StoreID = storeID;
        GoodsName = goodsName;
    }

    public static final Creator<GoodsDetailModel> CREATOR = new Creator<GoodsDetailModel>() {
        @Override
        public GoodsDetailModel createFromParcel(Parcel in) {
            return new GoodsDetailModel(in);
        }

        @Override
        public GoodsDetailModel[] newArray(int size) {
            return new GoodsDetailModel[size];
        }
    };

    public String getTitleSKU() {
        return titleSKU;
    }

    public void setTitleSKU(String titleSKU) {
        this.titleSKU = titleSKU;
    }

    public String getStrSKU(int type) {
        if (TextUtils.isEmpty(getSKU())) {
            return "";
        }
        switch (type) {
            case 0:
                splitChar = "、";
                break;
            case 1:
                splitChar = "+";
                break;
            default:
                break;

        }
        List<SKUPar> pars = GsonUtils.fromLocalJson(getSKU(), new TypeToken<List<SKUPar>>() {
        }.getType());
        StringBuilder tempStr = new StringBuilder();
        StringBuilder titleStr = new StringBuilder();
        for (SKUPar par : pars) {
            if (par.getSKUValueList() != null && par.getSKUValueList().size() != 0)
                tempStr.append(par.getSKUValueList().get(0).getSKUValue()).append(splitChar);
            titleStr.append(par.getSkuName()).append(splitChar);
        }
        if (tempStr.length() == 0 || titleStr.length() == 0){
            return "";
        }
        strSKU = tempStr.substring(0, tempStr.length() - 1);
        titleSKU = titleStr.substring(0, titleStr.length() - 1);
        return strSKU;
    }

    public void setStrSKU(String strSKU) {
        this.strSKU = strSKU;
    }

    public String SKUJson() {
        if (TextUtils.isEmpty(strSKU)) {
            return "";
        }
        String[] temp = strSKU.split(splitChar);
        String[] tempTitle = getTitleSKU().split(splitChar);
        List<SKUPar> tempPars = new ArrayList<>();
        for (int i = 0; i < tempTitle.length; i++) {
            SKUPar par = new SKUPar();
            par.setSkuName(tempTitle[i]);
            if (tempTitle[i].equals("规格")) {
                par.setSKUValueList(Collections.singletonList(new SKUPar.SKUValueListBean(temp[i], getDisCountPrice())));
            } else {
                par.setSKUValueList(Collections.singletonList(new SKUPar.SKUValueListBean(temp[i], "0")));
            }
            tempPars.add(par);
        }
        return GsonUtils.toJson(tempPars);
    }

    public String getServiceContent() {
        return ServiceContent;
    }

    public void setServiceContent(String serviceContent) {
        ServiceContent = serviceContent;
    }

    public String getTermOfValidity() {
        return TermOfValidity;
    }

    public void setTermOfValidity(String termOfValidity) {
        TermOfValidity = termOfValidity;
    }

    public String getUsageRules() {
        return UsageRules;
    }

    public void setUsageRules(String usageRules) {
        UsageRules = usageRules;
    }

    public String getRefundRules() {
        return RefundRules;
    }

    public void setRefundRules(String refundRules) {
        RefundRules = refundRules;
    }

    public String getSKU() {
        return SKU;
    }

    public void setSKU(String SKU) {
        this.SKU = SKU;
    }

    public List<GoodsSKUModel> getGoodsSKUModels() {
        return goodsSKUModels;
    }

    public void setGoodsSKUModels(List<GoodsSKUModel> goodsSKUModels) {
        this.goodsSKUModels = goodsSKUModels;
    }

    public String getCMemo() {
        return CMemo;
    }

    public void setCMemo(String CMemo) {
        this.CMemo = CMemo;
    }

    public String getDistributionFee() {
        if (TextUtils.isEmpty(DistributionFee)) {
            return "0";
        }
        return DistributionFee;
    }

    public void setDistributionFee(String distributionFee) {
        DistributionFee = distributionFee;
    }

    public String getStartPrice() {
        return StartPrice;
    }

    public void setStartPrice(String startPrice) {
        StartPrice = startPrice;
    }

    public String getPackingFee() {
        return PackingFee;
    }

    public void setPackingFee(String packingFee) {
        PackingFee = packingFee;
    }

    public String getShowOrder() {
        return ShowOrder;
    }

    public void setShowOrder(String showOrder) {
        ShowOrder = showOrder;
    }

    public String getGoodsNum() {
        return GoodsNum;
    }

    public void setGoodsNum(String goodsNum) {
        GoodsNum = goodsNum;
    }

    public String getShopCartID() {
        return ShopCartID;
    }

    public void setShopCartID(String shopCartID) {
        ShopCartID = shopCartID;
    }

    public String getGoodsID() {
        return GoodsID;
    }

    public void setGoodsID(String goodsID) {
        GoodsID = goodsID;
    }

    public String getCreateTime() {
        if (!TextUtils.isEmpty(CreateTime)) {
            CreateTime = CreateTime.replace("T", " ");
        }
        return CreateTime;
    }

    public void setCreateTime(String createTime) {
        CreateTime = createTime;
    }

    public String getCoverUrl() {
        return CoverUrl;
    }

    public void setCoverUrl(String coverUrl) {
        CoverUrl = coverUrl;
    }

    public boolean isCheck() {
        return isCheck;
    }

    public void setCheck(boolean check) {
        isCheck = check;
    }

    public String getClassID() {
        return ClassID;
    }

    public void setClassID(String classID) {
        ClassID = classID;
    }

    public String getClassName() {
        return ClassName;
    }

    public void setClassName(String className) {
        ClassName = className;
    }

    public String getLimitCount() {
        if (TextUtils.isEmpty(LimitCount)) {
            return "0";
        }
        return LimitCount;
    }

    public void setLimitCount(String limitCount) {
        LimitCount = limitCount;
    }

    public int getIsRecommends() {
        return IsRecommends;
    }

    public void setIsRecommends(int isRecommends) {
        IsRecommends = isRecommends;
    }

    public String getID() {
        return ID;
    }

    public void setID(String ID) {
        this.ID = ID;
    }

    public String getTitle() {
        return Title;
    }

    public void setTitle(String Title) {
        this.Title = Title;
    }

    public String getDisCountNum() {
        if (TextUtils.isEmpty(DisCountNum)) {
            return 0 + "";
        }
        return MyUtils.subZeroAndDot(DisCountNum);
    }

    public void setDisCountNum(String DisCountNum) {
        this.DisCountNum = DisCountNum;
    }

    public String getMonthSaleNum() {
        return MonthSaleNum;
    }

    public void setMonthSaleNum(String MonthSaleNum) {
        this.MonthSaleNum = MonthSaleNum;
    }

    public String getIPrice() {
        if (TextUtils.isEmpty(IPrice)) {
            return "0";
        }
        return MyUtils.subZeroAndDot(IPrice);
    }

    public void setIPrice(String IPrice) {
        this.IPrice = IPrice;
    }

    public String getStatu() {
        return Statu;
    }

    public void setStatu(String Statu) {
        this.Statu = Statu;
    }

    public String getShareTitle() {
        return ShareTitle;
    }

    public void setShareTitle(String ShareTitle) {
        this.ShareTitle = ShareTitle;
    }

    public String getShareImg() {
        return ShareImg;
    }

    public void setShareImg(String ShareImg) {
        this.ShareImg = ShareImg;
    }

    public String getShareDesc() {
        return ShareDesc;
    }

    public void setShareDesc(String ShareDesc) {
        this.ShareDesc = ShareDesc;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List<ImageModel> getImgList() {
        return ImgList;
    }

    public void setImgList(List<ImageModel> ImgList) {
        this.ImgList = ImgList;
    }

    public List<ImageModel> getContentImgList() {
        return ContentImgList;
    }

    public void setContentImgList(List<ImageModel> ContentImgList) {
        this.ContentImgList = ContentImgList;
    }

    public String getDisCountPrice() {
        if (TextUtils.isEmpty(DisCountPrice)) {
            return "0";
        }
        return MyUtils.subZeroAndDot(DisCountPrice);
    }

    public void setDisCountPrice(String disCountPrice) {
        DisCountPrice = disCountPrice;
    }

    public int getNum() {
        return Num;
    }

    public void setNum(int num) {
        Num = num;
    }

    public String getStoreID() {
        return StoreID;
    }

    public void setStoreID(String storeID) {
        StoreID = storeID;
    }

    public String formatPrice() {
        if (TextUtils.isEmpty(DisCountPrice)) {
            return 0 + "";
        }
        return MyUtils.subZeroAndDot(DisCountPrice);
    }

    public String getGoodsName() {
        return GoodsName;
    }

    public void setGoodsName(String goodsName) {
        GoodsName = goodsName;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {

        dest.writeString(ID);
        dest.writeString(Title);
        dest.writeString(DisCountNum);
        dest.writeString(MonthSaleNum);
        dest.writeString(IPrice);
        dest.writeString(Statu);
        dest.writeString(ShareTitle);
        dest.writeString(ShareImg);
        dest.writeString(ShareDesc);
        dest.writeString(url);
        dest.writeString(LimitCount);
        dest.writeInt(IsRecommends);
        dest.writeByte((byte) (isCheck ? 1 : 0));
        dest.writeString(DisCountPrice);
        dest.writeInt(Num);
        dest.writeString(StoreID);
        dest.writeString(GoodsID);
        dest.writeString(ShopCartID);
        dest.writeString(GoodsName);
        dest.writeString(GoodsNum);
        dest.writeString(ShowOrder);
        dest.writeString(PackingFee);
        dest.writeString(CMemo);
        dest.writeString(strSKU);
        dest.writeString(titleSKU);
    }

    public String getText() {
//        model.disCountNum+`折    限`+model.limitCount+`份`
        String s = "";
        if (Double.valueOf(DisCountNum) != 10) {
            if (Double.valueOf(LimitCount) != 0) {
                s = DisCountNum + "折    限" + LimitCount + "份";
            } else {
                s = DisCountNum + "折    不限购";
            }
        } else {
            if (Double.valueOf(LimitCount) != 0) {
                s = "原价    限" + LimitCount + "份";
            } else {
                s = "原价    不限购";
            }
        }
        return s;
    }
}